<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>版本 | 课程介绍</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C05-Components/Exceptions.html" />
    
    
    <link rel="prev" href="../C05-Components/Pagination.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="5.6"
        data-chapter-title="版本"
        data-filepath="C05-Components/Versioning.md"
        data-basepath=".."
        data-revision="Thu May 10 2018 07:57:11 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-IntroduceToDRF/index.html">
            
                
                    <a href="../C01-IntroduceToDRF/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        引入DjangoRESTframework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
            
                
                    <a href="../C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-IntroduceToDRF/IntroduceToREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/IntroduceToREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-IntroduceToDRF/HowToDesignREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/HowToDesignREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        RESTful设计方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
            
                
                    <a href="../C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        使用Django开发REST接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
            
                
                    <a href="../C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="C01-IntroduceToDRF/AboutDRF.html">
            
                
                    <a href="../C01-IntroduceToDRF/AboutDRF.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        DjangoRESTframework简介
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-DRFProject/index.html">
            
                
                    <a href="../C02-DRFProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        DRF工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-DRFProject/InstallAndConfig.html">
            
                
                    <a href="../C02-DRFProject/InstallAndConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-DRFProject/TheFirstDRFProgram.html">
            
                
                    <a href="../C02-DRFProject/TheFirstDRFProgram.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        见识DRF的魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Serializer/index.html">
            
                
                    <a href="../C03-Serializer/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Serializer序列化器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Serializer/Declaring.html">
            
                
                    <a href="../C03-Serializer/Declaring.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        定义Serializer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Serializer/Serializing.html">
            
                
                    <a href="../C03-Serializer/Serializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Serializer/Deserializing.html">
            
                
                    <a href="../C03-Serializer/Deserializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        反序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Serializer/ModelSerializer.html">
            
                
                    <a href="../C03-Serializer/ModelSerializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-View/index.html">
            
                
                    <a href="../C04-View/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-View/RequestAndResponse.html">
            
                
                    <a href="../C04-View/RequestAndResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Request与Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-View/View.html">
            
                
                    <a href="../C04-View/View.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        视图概览
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-View/ViewIntroduction.html">
            
                
                    <a href="../C04-View/ViewIntroduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        视图说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-View/ViewSet.html">
            
                
                    <a href="../C04-View/ViewSet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        视图集ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="C04-View/Routers.html">
            
                
                    <a href="../C04-View/Routers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-Components/index.html">
            
                
                    <a href="../C05-Components/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        其他功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-Components/Authentication.html">
            
                
                    <a href="../C05-Components/Authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        认证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-Components/Permissions.html">
            
                
                    <a href="../C05-Components/Permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        权限
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C05-Components/Throttling.html">
            
                
                    <a href="../C05-Components/Throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        限流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="C05-Components/Filtering.html">
            
                
                    <a href="../C05-Components/Filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        过滤
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="C05-Components/Pagination.html">
            
                
                    <a href="../C05-Components/Pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="5.6" data-path="C05-Components/Versioning.html">
            
                
                    <a href="../C05-Components/Versioning.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        版本
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="C05-Components/Exceptions.html">
            
                
                    <a href="../C05-Components/Exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="C05-Components/Documents.html">
            
                
                    <a href="../C05-Components/Documents.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自动生成接口文档
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程介绍</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x7248;&#x672C;versioning">&#x7248;&#x672C;Versioning</h1>
<p>REST framework&#x63D0;&#x4F9B;&#x4E86;&#x7248;&#x672C;&#x53F7;&#x7684;&#x652F;&#x6301;&#x3002;</p>
<p>&#x5728;&#x9700;&#x8981;&#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x7684;&#x7248;&#x672C;&#x53F7;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>request.version</code>&#x6765;&#x83B7;&#x53D6;&#x3002;</p>
<p>&#x9ED8;&#x8BA4;&#x7248;&#x672C;&#x529F;&#x80FD;&#x672A;&#x5F00;&#x542F;&#xFF0C;<code>request.version</code> &#x8FD4;&#x56DE;None&#x3002;</p>
<p>&#x5F00;&#x542F;&#x7248;&#x672C;&#x652F;&#x6301;&#x529F;&#x80FD;&#xFF0C;&#x9700;&#x8981;&#x5728;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x8BBE;&#x7F6E;<code>DEFAULT_VERSIONING_CLASS</code></p>
<pre><code class="lang-python">REST_FRAMEWORK = {
    <span class="hljs-string">&apos;DEFAULT_VERSIONING_CLASS&apos;</span>: <span class="hljs-string">&apos;rest_framework.versioning.NamespaceVersioning&apos;</span>
}
</code></pre>
<p>&#x5176;&#x4ED6;&#x53EF;&#x9009;&#x914D;&#x7F6E;&#xFF1A;</p>
<ul>
<li><strong>DEFAULT_VERSION</strong>  &#x9ED8;&#x8BA4;&#x7248;&#x672C;&#x53F7;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;None</li>
<li><strong>ALLOWED_VERSIONS</strong>  &#x5141;&#x8BB8;&#x8BF7;&#x6C42;&#x7684;&#x7248;&#x672C;&#x53F7;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;None</li>
<li><strong>VERSION_PARAM</strong>  &#x8BC6;&#x522B;&#x7248;&#x672C;&#x53F7;&#x53C2;&#x6570;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;&apos;version&apos;</li>
</ul>
<h2 id="&#x652F;&#x6301;&#x7684;&#x7248;&#x672C;&#x5904;&#x7406;&#x65B9;&#x5F0F;">&#x652F;&#x6301;&#x7684;&#x7248;&#x672C;&#x5904;&#x7406;&#x65B9;&#x5F0F;</h2>
<p><strong>1&#xFF09; AcceptHeaderVersioning</strong></p>
<p>&#x8BF7;&#x6C42;&#x5934;&#x4E2D;&#x4F20;&#x9012;&#x7684;Accept&#x643A;&#x5E26;version</p>
<pre><code class="lang-python">GET /bookings/ HTTP/<span class="hljs-number">1.1</span>
Host: example.com
Accept: application/json; version=<span class="hljs-number">1.0</span>
</code></pre>
<p><strong>2&#xFF09;URLPathVersioning</strong></p>
<p>URL&#x8DEF;&#x5F84;&#x4E2D;&#x643A;&#x5E26;</p>
<pre><code class="lang-python">urlpatterns = [
    url(
        <span class="hljs-string">r&apos;^(?P&lt;version&gt;(v1|v2))/bookings/$&apos;</span>,
        bookings_list,
        name=<span class="hljs-string">&apos;bookings-list&apos;</span>
    ),
    url(
        <span class="hljs-string">r&apos;^(?P&lt;version&gt;(v1|v2))/bookings/(?P&lt;pk&gt;[0-9]+)/$&apos;</span>,
        bookings_detail,
        name=<span class="hljs-string">&apos;bookings-detail&apos;</span>
    )
]
</code></pre>
<p><strong>3&#xFF09;NamespaceVersioning</strong></p>
<p>&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E2D;&#x5B9A;&#x4E49;</p>
<pre><code class="lang-python"><span class="hljs-comment"># bookings/urls.py</span>
urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, bookings_list, name=<span class="hljs-string">&apos;bookings-list&apos;</span>),
    url(<span class="hljs-string">r&apos;^(?P&lt;pk&gt;[0-9]+)/$&apos;</span>, bookings_detail, name=<span class="hljs-string">&apos;bookings-detail&apos;</span>)
]

<span class="hljs-comment"># urls.py</span>
urlpatterns = [
    url(<span class="hljs-string">r&apos;^v1/bookings/&apos;</span>, include(<span class="hljs-string">&apos;bookings.urls&apos;</span>, namespace=<span class="hljs-string">&apos;v1&apos;</span>)),
    url(<span class="hljs-string">r&apos;^v2/bookings/&apos;</span>, include(<span class="hljs-string">&apos;bookings.urls&apos;</span>, namespace=<span class="hljs-string">&apos;v2&apos;</span>))
]
</code></pre>
<p><strong>4&#xFF09;HostNameVersioning</strong></p>
<p>&#x4E3B;&#x673A;&#x57DF;&#x540D;&#x643A;&#x5E26;</p>
<pre><code class="lang-python">GET /bookings/ HTTP/<span class="hljs-number">1.1</span>
Host: v1.example.com
Accept: application/json
</code></pre>
<p><strong>5&#xFF09;QueryParameterVersioning</strong></p>
<p>&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x643A;&#x5E26;</p>
<pre><code class="lang-python">GET /something/?version=0.1 HTTP/1.1
Host: example.com
Accept: application/json
</code></pre>
<h3 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h3>
<pre><code class="lang-python">REST_FRAMEWORK = {
    <span class="hljs-string">&apos;DEFAULT_VERSIONING_CLASS&apos;</span>: <span class="hljs-string">&apos;rest_framework.versioning.QueryParameterVersioning&apos;</span>
}
</code></pre>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoSerializer</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = BookInfo
        fields = (<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;btitle&apos;</span>, <span class="hljs-string">&apos;bpub_date&apos;</span>, <span class="hljs-string">&apos;bread&apos;</span>, <span class="hljs-string">&apos;bcomment&apos;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoSerializer2</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = BookInfo
        fields = (<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;btitle&apos;</span>, <span class="hljs-string">&apos;bpub_date&apos;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookDetailView</span><span class="hljs-params">(RetrieveAPIView)</span>:</span>
    queryset = BookInfo.objects.all()

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_serializer_class</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">if</span> self.request.version == <span class="hljs-string">&apos;1.0&apos;</span>:
            <span class="hljs-keyword">return</span> BookInfoSerializer
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> BookInfoSerializer2

<span class="hljs-comment"># 127.0.0.1:8000/books/2/</span>
<span class="hljs-comment"># 127.0.0.1:8000/books/2/?version=1.0</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C05-Components/Pagination.html" class="navigation navigation-prev " aria-label="Previous page: 分页"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C05-Components/Exceptions.html" class="navigation navigation-next " aria-label="Next page: 异常处理"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
